package org.ehe.erp.service.purchase.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.oss.entity.UploadResult;
import org.ehe.erp.domain.purchase.ErpPurchaseOrderAttachment;
import org.ehe.erp.domain.purchase.vo.ErpPurchaseOrderAttachmentVo;
import org.ehe.erp.domain.quality.ErpQualityAttachment;
import org.ehe.erp.domain.quality.vo.ErpQualityAttachmentVo;
import org.ehe.erp.mapper.purchase.ErpPurchaseOrderAttachmentMapper;
import org.ehe.erp.service.purchase.IErpPurchaseOrderAttachmentService;
import org.ehe.system.domain.vo.SysOssVo;
import org.ehe.system.service.ISysOssService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 采购附件serviceImpl
 * @author :zhangnn
 * @className :ErpPurchaseOrderAttachmentServiceImpl
 * @description: TODO
 * @date 2025-09-23 09:38:58
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ErpPurchaseOrderAttachmentServiceImpl extends ServiceImpl<ErpPurchaseOrderAttachmentMapper, ErpPurchaseOrderAttachment>
    implements IErpPurchaseOrderAttachmentService {

    private final ISysOssService ossService;

    /**
     * 上传
     * @param file
     * @return
     */
    @Override
    public UploadResult upload(MultipartFile file) {
        SysOssVo upload = ossService.upload(file);
        SysOssVo sysOssVo = ossService.getById(upload.getOssId());
        return UploadResult.builder()
            .filename(sysOssVo.getOriginalName())
            .eTag(sysOssVo.getFileSuffix())
            .url(sysOssVo.getUrl())
            .build();
    }

    /**
     * 根据ID查询
     * @param orderId
     * @return
     */
    @Override
    public List<ErpPurchaseOrderAttachmentVo> listById(Long orderId) {
        return lambdaQuery().eq(ErpPurchaseOrderAttachment::getPurchaseOrderId, orderId)
            .orderByDesc(ErpPurchaseOrderAttachment::getUploadTime)
            .list()
            .stream()
            .map(a -> BeanUtil.copyProperties(a, ErpPurchaseOrderAttachmentVo.class))
            .toList();
    }

    /**
     * 删除信息
     * @param ids
     * @return
     */
    @Override
    public int deleteWithValidByIds(List<Long> ids) {
        ids.forEach(id->{
            this.baseMapper.deleteById(id);
        });
        return 0;
    }
}
